//
//  AppAlertTool.m
//  MyAppTool
//
//  Created by Apple on 2017/12/6.
//  Copyright © 2017年 yxkj. All rights reserved.
//

#import "AppAlertTool.h"

#ifndef SYSTEM_VERSION
#define SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#endif

#ifndef KiOS8Later
#define KiOS8Later (SYSTEM_VERSION >= 8)
#endif



@interface AppAlertTool()<UIAlertViewDelegate>

@end

@implementation AppAlertTool

+(void)lh_alterViewWithTitle:(NSString*_Nonnull)title AndContentString:(NSString*_Nonnull)string AndComfirmBtnTitle:(NSString*_Nonnull)btnTitle AndCancelTitle:(NSString*_Nullable)cancelTitle AndShow:(UIViewController*_Nonnull)ctrl AndComfirmBlock:(comfirmBlock _Nullable )block AndCancelBlock:(cancelBlock _Nullable )cancelBlock{
    
#ifdef KiOS8Later
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:string preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action=[UIAlertAction actionWithTitle:btnTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
         block();
        
    }];
    UIAlertAction *cancelBtn=[UIAlertAction actionWithTitle:cancelTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
         cancelBlock();
        
    }];
    [alert addAction:action];
    [alert addAction:cancelBtn];
    [ctrl  presentViewController:alert animated:YES completion:nil];
#else
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:title message:string delegate:self cancelButtonTitle:btnTitle otherButtonTitles:cancelTitle];
    [alertview show];
#endif
}

#ifdef KiOS8Later
#else
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        if (self.canBlock) {
            self.canBlock();
        }
    }else if(buttonIndex==1){
        if (self.comBlock) {
            self.comBlock();
        }
    }
}
#endif


#pragma mark - 系统弹窗输入框
+(void)lh_alterViewWithTextField:(NSString*_Nullable)title AndContentString:(NSString*_Nullable)string AndComfirmBtnTitle:(NSString*_Nonnull)btnTitle AndCancelTitle:(NSString*_Nullable)cancelTitle AndShow:(UIViewController*_Nonnull)ctrl AndComfirmBlock:(textFieldComfirmBlock _Nullable )block AndCancelBlock:(textFieldCancelBlock _Nullable )cancelBlock AndCompletion:(completionBlock _Nullable )cBlock AndKeyBorderType:(UIKeyboardType)type AndFieldPlaceHolder:(NSString*_Nullable)placeStr{
    #ifdef KiOS8Later
    UIAlertController*vc= [UIAlertController alertControllerWithTitle:title message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* act=[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancelBlock) {
            cancelBlock();
        }
    }];
    UIAlertAction* actio=[UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (block) {
            NSString*str=[vc.textFields firstObject].text;
            block(str);
        }
    }];
    [vc addAction:act];
    [vc addAction:actio];
    [vc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder=placeStr;
        textField.keyboardType=type;
    }];
    [ctrl presentViewController:vc animated:YES completion:^{
        if (cBlock) {
            cBlock();
        }
    }];
    #endif
}
@end
